const top250 = [];
let currentPage = 1;
const iframe = document.createElement("iframe");
iframe.onload = () => {
  const iDom = iframe.contentWindow.document;
  const itemList = iDom.querySelectorAll(".item");
  for (let i = 0; i < itemList.length; i++) {
    const item = itemList[i];
    // 电影详情链接
    const link = item.querySelector("a").href;
    // 图片链接
    const imageUrl = item.querySelector("img").src;
    // 影片名字
    const name = item.querySelector(".title").innerText;
    // 评分
    const rateNum = item.querySelector(".rating_num").innerText;
    //评价数
    const ratePerson = parseInt(
      item.querySelector(".star").lastChild.previousSibling.innerText
    );
    // 概况
    const about = item.querySelector(".quote")?.innerText;
    // 相关信息
    const desc = item.querySelector(".bd").querySelector("p").innerText;
    top250.push({ link, imageUrl, name, rateNum, ratePerson, about, desc })
  }

  // 启动下一页
  if (currentPage < 10) {
    currentPage++;
    iframe.src = `https://movie.douban.com/top250?start=${25 * (currentPage - 1)}`;
  } else {
    downloadText("test.json", JSON.stringify(top250));
  }
};
document.body.appendChild(iframe);
iframe.src = "https://movie.douban.com/top250?start=0";

function downloadText(fileName, text) {
  const url = window.URL || window.webkitURL || window;
  const blob = new Blob([text]);
  const saveLink = document.createElement("a");
  saveLink.href = url.createObjectURL(blob);
  // 设置 download 属性
  saveLink.download = fileName+ '.json';;
  saveLink.click();
  url.revokeObjectURL(saveLink.href);
  saveLink?.remove()
}
